package com.woniuxy.servicelayer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.dal.entity.Userinfo;
import com.woniuxy.dal.mapper.UserinfoMapper;

import com.woniuxy.servicelayer.UserinfoService;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.woniuxy.servicelayer.util.Md5Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author rjq51
 * @description 针对表【userInfo(用户表)】的数据库操作Service实现
 * @createDate 2023-03-27 17:02:43
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
        implements UserinfoService {

    @Resource
    UserinfoMapper userinfoMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;


    @Override
    public int modifyImgsURL(String phoneNumber, String photoName) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("phoneNumber", phoneNumber);
        Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);
        userinfo.setUserIcon(photoName);
        return userinfoMapper.updateById(userinfo);

    }

    @Override
    public Userinfo checkLogin(String phoneNumber, String password) {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phoneNumber", phoneNumber);
        queryWrapper.eq("password", password);
        Userinfo userinfo = userinfoMapper.selectOne(queryWrapper);
        return userinfo;
    }


    /**
     * 用户修改
     *
     * @return
     */
    @Override
    public int updateUserInfo(Userinfo userinfo) {
        return userinfoMapper.updateById(userinfo);
    }

    /**
     * 查询用户信息 返回分页结果
     */
    @Override
    public Page<Userinfo> selectUserInfo(Integer currentPage, Integer pageSize, String keyword) {
        Page<Userinfo> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            userinfoQueryWrapper.like("username", keyword);
        }
        return userinfoMapper.selectPage(page, userinfoQueryWrapper);
    }

    /**
     * 添加用户
     *
     * @param userinfo
     * @return
     */

    @Override
    public int addUser(Userinfo userinfo) {
        return userinfoMapper.insert(userinfo);
    }

    @Override
    public int deleteUser(Integer uesrId) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId", uesrId);
        return userinfoMapper.delete(userinfoQueryWrapper);
    }

    /**
     * 重置密码 为123456
     *
     * @param userId
     * @param
     * @return
     */
    @Override
    public String resetPassword(Integer userId) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId", userId);
        Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);
        //密码加密
        String password ="Wn" + (int) ((Math.random() * 9 + 1) * 1000) + userinfo.getPhoneNumber().substring(7);
        String encode = Md5Util.encode(password);
        userinfo.setPassword(encode);
        //修改密码
        userinfoMapper.updateById(userinfo);
        return password;
    }


    /**
     * 根据用户id 赋予角色
     *
     * @param userId
     * @param permission
     * @return
     */
    @Override
    public int personalize(Integer userId, Integer permission) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId", userId);
        Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);
        userinfo.setPermission(permission);

        return userinfoMapper.updateById(userinfo);
    }

    /**
     * 修改密码 填写的原密码
     *
     * @param userId
     * @param password
     * @return
     */
    @Override
    public String oldPassword(Integer userId, String password) {
        String encode = Md5Util.encode(password);
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId", userId);
        Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);
        if (userinfo.getPassword().equals(encode)) {
            return "ok";
        }
        return "密码不匹配";

    }

    @Override
    public Userinfo selectUserById(Integer userId) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        QueryWrapper<Userinfo> qw = userinfoQueryWrapper.eq("userId", userId);
        return userinfoMapper.selectOne(qw);
    }


    /**
     * 查询手机号是否存在
     *
     * @param phone
     * @return
     */
    @Override
    public Userinfo getUserByAccount(String phone) {

        if (StringUtils.isEmpty(phone)) {
            return null;
        }

        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("phoneNumber", phone);

        Userinfo userinfo = userinfoMapper.selectOne(userinfoQueryWrapper);

        return userinfo;
    }

    /**
     * 根据userid查询该用户对应的角色
     *
     * @param userId
     * @return
     */
    @Override
    public Userinfo getThisRole(Integer userId) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId",userId);
        return userinfoMapper.selectOne(userinfoQueryWrapper);
    }

    /**
     * 通过手机号查找用户
     */
    @Override
    public Userinfo phoneNumberToUser(String phoneNumber) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("phoneNumber",phoneNumber);
        return userinfoMapper.selectOne(userinfoQueryWrapper);
    }

    @Override
    public Integer updateUserNameById(Object userId, String newUserName) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId",userId);
        return userinfoMapper.updateUserNameById(userId,newUserName);
    }

    @Override
    public Integer updateUserPassById(Object userId, String newUserPass) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId",userId);
        return userinfoMapper.updateUserPassById(userId,newUserPass);
    }

    @Override
    public Integer updateUserPhoneNumber(String phoneNumber, Integer userId) {
        QueryWrapper<Userinfo> userinfoQueryWrapper = new QueryWrapper<>();
        userinfoQueryWrapper.eq("userId",userId);
        return userinfoMapper.updatePhoneNumberById(userId,phoneNumber);
    }
}







